<?php

session_start();

$_SMARTY_DIR = '/lib/Smarty3.0beta7/';

ini_set( 'include_path', ini_get('include_path') .
        PATH_SEPARATOR . getcwd() .$_SMARTY_DIR.
        PATH_SEPARATOR . getcwd() .'/lib/utils/'.
        PATH_SEPARATOR . getcwd() .'/lib/users/'.
        PATH_SEPARATOR . getcwd() .'/lib/'.
        PATH_SEPARATOR . getcwd() .'/src/'.
        PATH_SEPARATOR . getcwd() .'/lib/design/'.
        PATH_SEPARATOR . getcwd() .'/lib/twitter-oauth/'.
        PATH_SEPARATOR . getcwd() .'/lib/PHPMailer_v5.1/'.
        PATH_SEPARATOR . getcwd() .'/lib/chat/'.
        PATH_SEPARATOR . getcwd() .'/lib/friends/'.
        PATH_SEPARATOR . getcwd() .'/lib/games/'.
        PATH_SEPARATOR . getcwd() .'/lib/tournament/'.
        PATH_SEPARATOR . getcwd() .'/handjobs/'.
        PATH_SEPARATOR . getcwd() .'/lib/db/'
        );

require_once( 'config.php' );

date_default_timezone_set( $_timezone );

require_once( 'functions.php' );
require_once( 'handy_user_class_extended.inc.php' );
require_once( 'db_class.inc.php' );
require_once( 'memcache.inc.php' );
require_once( 'lib/design/design.inc.php' );
require_once( 'lib/chat/chat.inc.php' );
require_once( 'lib/friends/friends.inc.php' );
require_once( 'lib/games/games.inc.php' );
require_once( 'lib/tournament/tournament.inc.php' );
require_once( 'lib/PHPMailer_v5.1/class.phpmailer.php' );								

define( 'SMARTY_DIR', $_base_path .$_SMARTY_DIR );
require_once( SMARTY_DIR .'/Smarty.class.php' );

if ( $_debug )
{
    error_reporting( E_ALL );
    $_main_time_start = microtime( true );
}
else
{
    error_reporting( 0 );
    set_error_handler( 'error_handler' );
}

/* Initializing Smarty */
$_main_page = get_smarty();

/* Initializing DB_MySQL */
$DB = new DB_mysql( $_mysql_host, $_mysql_user, $_mysql_pass, $_mysql_name, $_debug );

/* Initializing User Class */
$User = new user_class_extended( $DB );

/* Chat class */
$Chat = new chat( $DB );

/* Friends class */
$Friends = new friends( $DB );

/* Gameslist class */
$Games = new games( $DB );

/* Tournament class */
$Tournament = new tournament( $DB );

if ( $_design )
{
    /* Initializing Design Monitoring */
    $Design = new Design_monitor( $DB, isset( $_SESSION['design_vizitator_id'] ) ? $_SESSION['design_vizitator_id'] : null, $User->USER_ID );
    if ( $Design )
    {
            $_SESSION['design_vizitator_id'] = $Design->Vizitator_id;
    }
}
$URL = break_url();

$layout = 'site';
$_page_content = '';
$_output = true;

$_footer_page = get_smarty();

$_footer = $_footer_page->fetch( '_footer.tpl' );
switch( $URL[0] )
{
    case '':
    default:
        $layout = 'site';
        include_once( 'src/home.php' );
        break;
        
    case 'user':
    	$layout = 'site';
    	include_once( 'src/user.php' );
    	break;
		
	case 'myprofile':
    	$layout = 'site';
    	include_once( 'src/myprofile.php' );
    	break;
    	
    case 'chat':
    	include_once( 'src/chat.php' );
    	break;
    	
    case 'chat-public':
    	$layout = 'site';
    	include_once( 'src/chat_test.php' );
    	break;
    	
    case 'friend-test':
    	$layout = 'site';
    	include_once( 'src/friend_test.php' );
    	break;
    
    case 'tournament':
    	$layout = 'site';
    	include_once( 'src/tournament.php' );
    	break;
    
}

if ( $_debug )
{
    $_run_time = microtime( true ) - $_main_time_start;
    $_main_page->assign( 'run_time', $_run_time );
}

$_main_page->assign( 'page_content', $_page_content );
$_main_page->assign( 'footer', $_footer );

// If it's a AJAX request, give the result json style
if ( !strtolower( @$_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' && $_output )
{
	$_main_page->display( '_layout_'. $layout .'.tpl' );
}
elseif ( $_output )
{
	$_ajax_result = array(
                                'error' => @$_ajax_message,
                                'data' => @$_ajax_data
                                 );
	if ( $_debug )
	{
		$_ajax_result['time'] = $_run_time;
	}
	echo json_encode( $_ajax_result );
}
else
{
	
}
	

?>